package cn.zjutleo.village_server.controller;

import cn.zjutleo.village_server.dto.PUStarandcolletKeyDto;
import cn.zjutleo.village_server.entity.PUStarandcollet;
import cn.zjutleo.village_server.service.PUStarandcolletService;
import cn.zjutleo.village_server.vo.ResponseBean;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/PUStarandcollet")
public class PUStarandcolletController {
    @Resource
    PUStarandcolletService puStarandcolletService;


    @PostMapping("/star")
    public ResponseBean<String> userStartPolicy(@RequestBody PUStarandcolletKeyDto puStarandcolletKeyDto) {
        if (puStarandcolletService.isContainPU(puStarandcolletKeyDto)) {
            PUStarandcollet puStarandcollet = new PUStarandcollet();
            BeanUtils.copyProperties(puStarandcolletKeyDto, puStarandcollet);
            puStarandcollet.setStar(true);
            puStarandcolletService.update(puStarandcollet);
            return new ResponseBean<>("success");
        } else {
            PUStarandcollet puStarandcollet = new PUStarandcollet();
            BeanUtils.copyProperties(puStarandcolletKeyDto, puStarandcollet);
            puStarandcollet.setStar(true);
            puStarandcolletService.insert(puStarandcollet);
            return new ResponseBean<>("success");
        }

    }

    @PostMapping("/collet")
    public ResponseBean<String> userColletPolicy(@RequestBody PUStarandcolletKeyDto puStarandcolletKeyDto) {
        if (puStarandcolletService.isContainPU(puStarandcolletKeyDto)) {
            PUStarandcollet puStarandcollet = new PUStarandcollet();
            BeanUtils.copyProperties(puStarandcolletKeyDto, puStarandcollet);
            puStarandcollet.setCollet(true);
            puStarandcolletService.update(puStarandcollet);
            return new ResponseBean<>("success");
        } else {
            PUStarandcollet puStarandcollet = new PUStarandcollet();
            BeanUtils.copyProperties(puStarandcolletKeyDto, puStarandcollet);
            puStarandcollet.setCollet(true);
            puStarandcolletService.insert(puStarandcollet);
            return new ResponseBean<>("success");
        }

    }

    @PostMapping("/cancelstar")
    public ResponseBean<String> userCancelStarPolicy(@RequestBody PUStarandcolletKeyDto puStarandcolletKeyDto) {
        if (puStarandcolletService.isContainPU(puStarandcolletKeyDto)) {
            PUStarandcollet puStarandcollet = new PUStarandcollet();
            BeanUtils.copyProperties(puStarandcolletKeyDto, puStarandcollet);
            puStarandcollet.setStar(false);
            puStarandcolletService.update(puStarandcollet);
            return new ResponseBean<>("success");
        } else {
            return new ResponseBean<>("关联信息不存在！");
        }
    }

    @PostMapping("/cancelcollet")
    public ResponseBean<String> userCancelColletPolicy(@RequestBody PUStarandcolletKeyDto puStarandcolletKeyDto) {
        if (puStarandcolletService.isContainPU(puStarandcolletKeyDto)) {
            PUStarandcollet puStarandcollet = new PUStarandcollet();
            BeanUtils.copyProperties(puStarandcolletKeyDto, puStarandcollet);
            puStarandcollet.setCollet(false);
            puStarandcolletService.update(puStarandcollet);
            return new ResponseBean<>("success");
        } else {
            return new ResponseBean<>("关联信息不存在！");
        }
    }
}
